package com.quyunshuo.module.home.activity.search

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.quyunshuo.androidbaseframemvvm.base.ktx.launchIO
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel
import com.quyunshuo.androidbaseframemvvm.network.bean.ArticleBean
import com.quyunshuo.androidbaseframemvvm.network.bean.ArticleList
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import javax.inject.Inject

/**
 * 首页的VM层
 *
 * @property mRepository HomeRepository 仓库层 通过Hilt注入
 *
 * @author Qu Yunshuo
 * @since 5/25/21 5:41 PM
 */
@HiltViewModel
class SearchViewModel @Inject constructor(private val mRepository: SearchRepository) : BaseViewModel() {

    val data = MutableLiveData<ArticleList>()
    private val dataList = mutableListOf<ArticleBean>()

    fun doSearch(page: Int, keyWord: String) {
        launchIO {
            mRepository.doSearch(page,keyWord)
                .catch { Log.d("qqq", "getData catch: $it") }
                .collect {
                    Log.d("qqq", "getData collect: ${it.data}")
                    data.postValue(it.data)
                }
        }

    }
}